<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
import { subOnLoad, changeEditor, autoChangeEditor, configSelectData } from '../../../static/js/formUtil.js';
import { ignoreHtml, ptFloat, minusF, getDateStr } from '@/static/js/common.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			initValue: {},
			autoformdata: [],
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			formdata: {},
			checkedit: false,
			frequencyObj: {},
			frequencyIdList: [],
			postId: '',
			initFlag: true,
			// businessCacheKey: 'adm-sta-prm-postDescription-work-title2-business-cache',
			keyUserCacheKey: 'adm-sta-prm-postDescription-work-title2-keyUser-cache',
			reviewUserPostCacheKey: 'adm-sta-prm-postDescription-work-title2-reviewUserPost-cache'
		};
	},
	watch: {},
	methods: {
		initLoad() {
			let _that = this;
			let frequencyId = _that.formdata.frequencyId;
			//频次
			_that.loadFrequency();
			//时限参数
			if (frequencyId && _that.frequencyIdList.indexOf(frequencyId) < 0) {
				//频次为“1次/X”的
				let dict = _that.frequencyObj;
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'num'
					},
					{
						type: 'labelbox',
						required: false,
					}
				);
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'dateParam'
					},
					{
						type: 'numberbox',
						min: dict.minNum,
						max: dict.maxNum
					}
				);
			} else {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'num'
					},
					{
						type: 'numberbox',
					}
				);
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'dateParam'
					},
					{
						type: 'labelbox',
						required: false,
						min: '',
						max: ''
					}
				);
			}

			//负责人
			autoChangeEditor(
				{
					obj: _that,
					key: 'title2',
					valueField: 'keyUserId'
				},
				{
					link: '/pages/public/search/search',
					linkParam: {
						other: JSON.stringify({
							posId: _that.postId
						}),
						// method:'post',
						namefield: 'username',
						likefield: 'username',
						// searchfield: 'q',
						initload: true,
						url: 'orgInstPosUserOfficial/findUserModelByPos',
						cachename: _that.keyUserCacheKey,
						placeholder: '请输入姓名'
					}
				}
			);

			//完成须评价
			_that.loadReview(frequencyId);
			//评价人
			_that.loadReviewUserPost(_that.formdata.reviewId);
			//分值
			_that.loadScore(_that.formdata.reviewId, _that.formdata.frequencyId);
			//取消
			if (_that.formdata.postDescriptionObjShowId) {
				_that.om
					.request('dictZero/getData', {
						code: '36925'
					})
					.then(res => {
						_that.$refs.autoform.changeEditor('title2', 'cancelId', 'data', configSelectData(res, 'id', 'dicname'));
						_that.$refs.autoform.initSelectIndex('title2', 'cancelId');
					});
			} else {
				//本次新增数据 禁止使用取消
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'cancelId'
					},
					{
						type: 'labelbox',
						required: false
					}
				);
			}
		},
		/**
		 * 加载频次选项
		 */
		loadFrequency() {
			let _that = this;
			_that.om.request('postDescription/findFrequencyList').then(res => {
				_that.$refs.autoform.changeEditor('title2', 'frequencyId', 'data', configSelectData(res, 'id', 'dicname'));
				_that.$refs.autoform.initSelectIndex('title2', 'frequencyId');
			});
		},
		/**
		 * 频次选中触发事件
		 * @param {Object} item 频次
		 */
		onSelectFrequency(item) {
			let frequencyId = item.id;
			let dict = item;
			let _that = this;
			_that.frequencyObj = item;
			_that.clearFrequency(true);
			//频次为“1次/X”的，次数默认赋值1
			if (_that.frequencyIdList.indexOf(frequencyId) < 0) {
				_that.$refs.autoform.formdata.num = 1;
				_that.$refs.autoform.changeEditor('title2', 'num', {
					type: 'labelbox',
					required: false
				});
				_that.$refs.autoform.changeEditor('title2', 'dateParam', {
					type: 'numberbox',
					min: dict.minNum,
					max: dict.maxNum
				});
			} else {
				_that.$refs.autoform.formdata.num = '';
				_that.$refs.autoform.changeEditor('title2', 'num', {
					type: 'numberbox',
				});
				_that.$refs.autoform.changeEditor('title2', 'dateParam', {
					type: 'labelbox',
					required: false,
					min: '',
					max: ''
				});
			}
			_that.loadReview(frequencyId);
			_that.loadReviewUserPost();
			_that.loadScore();
		},

		/**
		 * 清空频次选项
		 */
		clearFrequency(filterSelf) {
			let _that = this;
			if (!filterSelf) {
				_that.$refs.autoform.clear('title2', 'frequencyId');
			}
			let formdata = _that.$refs.autoform.formdata;
			formdata.num = '';
			formdata.dateParam = '';
			formdata.dateRemark = '';
			formdata.estimateTime = '';
			formdata.remarkStr = '';
			_that.$refs.autoform.clear('title2', 'keyUserId');
			_that.$refs.autoform.clear('title2', 'reviewId');
			_that.clearReviewUserPostData();
			formdata.score = '';
			_that.$refs.autoform.clear('title2', 'cancelId');
		},
		/**
		 * 时限参数 失去焦点事件
		 * @param {Object} newValue
		 */
		onSelectDateParam(newValue) {
			if (newValue) {
				let _that = this;
				var dict = _that.frequencyObj;
				if (dict) {
					//验证最大最小值
					if (dict.maxNum && dict.minNum) {
						var maxNum = ptFloat(dict.maxNum);
						var minNum = ptFloat(dict.minNum);
						if (ptFloat(newValue) > maxNum) {
							_that.om.toast('时限参数不能大于' + maxNum + '！');
							_that.$refs.autoform.formdata.dateParam = '';
						} else if (ptFloat(newValue) < minNum) {
							_that.om.toast('时限参数小于' + minNum + '！');
							_that.$refs.autoform.formdata.dateParam = '';
						} else {
							//拼接完成时限
							_that.om
								.request('postDescription/getDateRemark', {
									frequencyId: _that.$refs.autoform.formdata.frequencyId,
									dateParam: newValue
								})
								.then(res => {
									if (res.succeed) {
										var value = res.value;
										if (value) {
											_that.$refs.autoform.formdata.dateRemark = value;
										}
									} else {
										_that.om.toast(res.msg);
										_that.$refs.autoform.formdata.dateParam = '';
									}
								});
						}
					} else {
						_that.$refs.autoform.formdata.dateParam = '';
					}
				} else {
					_that.om.toast('请先选择频次！');
					_that.$refs.autoform.formdata.dateParam = '';
				}
			}
		},

		onSelectReview(item) {
			let _that = this;
			let reviewId = item ? item.id : '';
			_that.loadReviewUserPost(reviewId);
			_that.loadScore(reviewId, this.formdata.frequencyId);
		},
		loadReview(frequencyId) {
			let _that = this;
			if (frequencyId && _that.frequencyIdList.indexOf(frequencyId) < 0) {
				//频次为“1次/X”的
				_that.om
					.request('dictZero/getData', {
						code: '36925'
					})
					.then(res => {
						_that.$refs.autoform.changeEditor('title2', 'reviewId', {
							data: configSelectData(res, 'id', 'dicname'),
							type: 'selectbox'
						});
						_that.$refs.autoform.initSelectIndex('title2', 'reviewId');
					});
			} else {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'reviewId'
					},
					{
						type: 'labelbox',
						required: false
					}
				);
			}
		},
		loadReviewUserPost(reviewId) {
			let _that = this;
			//评价人
			if (reviewId) {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'reviewUserPostId'
					},
					{
						type: 'linkbox',
						link: '/pages/public/search/search',
						linkParam: {
							other: JSON.stringify({
								posId: _that.postId
							}),
							// method:'post',
							namefield: 'userPosName',
							likefield: 'userPosName',
							// searchfield: 'q',
							initload: true,
							url: 'orgInstPosUserOfficial/findListByOrgId',
							cachename: _that.reviewUserPostCacheKey,
							placeholder: '请输入姓名或岗位'
						}
					}
				);
			} else {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'reviewUserPostId'
					},
					{
						type: 'labelbox',
						required: false
					}
				);
			}
		},

		loadScore(reviewId, frequencyId) {
			let _that = this;
			//分值
			if (reviewId && frequencyId) {
				_that.om
					.request('postDescription/getDataDict', {
						frequencyId: frequencyId
					})
					.then(res => {
						if (res.succeed) {
							let data = res.value;
							if (!data) {
								data = {};
							}
							_that.$refs.autoform.changeEditor('title2', 'score', {
								type: 'numberbox',
								min: data.minNum,
								max: data.maxNum
							});
							_that.$refs.autoform.formdata.score = data.initNum;
						} else {
							_that.om.toast(res.msg);
							_that.$refs.autoform.clear('title2', 'reviewId');
						}
					});
			} else {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title2',
						valueField: 'score'
					},
					{
						type: 'labelbox',
						required: false,
						min: '',
						max: ''
					}
				);
			}
		},

		/**
		 * 清空评价人选项
		 */
		clearReviewUserPostData() {
			let _that = this;
			_that.$refs.autoform.clear('title2', 'reviewUserPostId');
			let formdata = _that.$refs.autoform.formdata;
			formdata.reviewUserPostId = '';
			formdata.reviewUserId = '';
			formdata.reviewUserName = '';
			formdata.reviewPostId = '';
			formdata.reviewPostName = '';
		},
		verifySubConfirm: function() {
			let _that = this;
			if (!_that.$refs.autoform.formdata.dutyRemark) {
				_that.om.toast('工作项名称(动词+名词)不能为空！');
				return false;
			}
			return true;
		},	
		verifySubDel: function() {
			let _that = this;
			if (_that.$refs.autoform.formdata.postDescriptionRemarkShowId) {
				_that.om.toast('只能清空本次新增的信息！');
				return false;
			}
			return true;
		}
	},
	onLoad(e) {
		let _that = this;
		subOnLoad(_that, e, function() {
			_that.frequencyIdList = e.frequencyIdList;
			_that.postId = e.postId;
			if (e.configDataCacheKey) {
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						//if (res.data && res.data instanceof Array) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data;
							Object.assign(_that.initValue, _that.formdata);
							_that.autoformdata[0].order = _that.order;
							if (_that.formdata.frequencyId) {
								//获取已有频次属性
								_that.om
									.request('dictZero/getObjById', {
										id: _that.formdata.frequencyId
									})
									.then(res => {
										if (res.succeed) {
											_that.frequencyObj = res.value;
											_that.initLoad();
										} else {
											_that.om.toast(res.msg);
										}
									});
							} else {
								_that.initLoad();
							}
						}
					}
				});
			}
		});
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			uni.getStorage({
				key: _that.keyUserCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.keyUserId = d.id;
						formdata.keyUserName = d.username;
						_that.clearReviewUserPostData();
						uni.removeStorage({
							key: _that.keyUserCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.reviewUserPostCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.reviewUserPostId = d.id;
						formdata.reviewUserId = d.userId;
						formdata.reviewUserName = d.userName;
						formdata.reviewPostId = d.posId;
						formdata.reviewPostName = d.posName;
						if (d.userId == _that.formdata.keyUserId) {
							_that.om.toast('作为“负责人”，不能再选择为“评价人”，请重新选择！');
							_that.clearReviewUserPostData();
						}
						uni.removeStorage({
							key: _that.reviewUserPostCacheKey
						});
					}
				}
			});
		}
	}
};
</script>

<style></style>
